package studio.raptor.dispatcher.server.service;

import java.util.Set;
import org.mockito.internal.util.collections.Sets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import studio.raptor.databus.command.EventMetaData;
import studio.raptor.dispatcher.server.filter.Subscription;

/**
 * 事件过滤服务
 *
 * @author jack
 * @since 1.0
 */
@Service
public class FilterService {

  private static final Logger log = LoggerFactory.getLogger(FilterService.class);

  @Autowired
  Subscription subscription;

  /**
   * 事件过滤,判断事件是否需要发送.
   *
   * @param code 接口编码
   * @param eventMeta 事件元信息
   * @return true:需要发送，false：不需要发送
   */
  public boolean filterEvent(String code, EventMetaData eventMeta) {
    String orderId = eventMeta.getBusinessNo();
    Set<String> codes = subscription.getSubscription(eventMeta);
    log.debug("event[{}],subscribed by {}", orderId, codes.toString());
    return codes.contains(code);
  }

}
